# 指定编译器
CC = aarch64-linux-gnu-gcc
CXX = aarch64-linux-gnu-g++

# 链接器标志，例如：-L/usr/lib -lm (指定库搜索路径，链接数学库)
LDFLAGS = -L/usr/lib
LDLIBS = -lm

# 指定头文件目录
INCLUDES = -I/usr/include

# 源文件和对象文件
C_SOURCES = $(wildcard *.c)
CPP_SOURCES = $(wildcard *.cpp)
C_OBJECTS = $(C_SOURCES:.c=.o)
CPP_OBJECTS = $(CPP_SOURCES:.cpp=.o)
OBJECTS = $(addprefix build/,$(C_OBJECTS) $(CPP_OBJECTS))

# 目标二进制文件名和目标目录
TARGET = frameParse
TARGET_DIR = build
INSTALL_DIR = install

TEST_TARGET = app
TEST_DIR = test
TEST_BIN = $(TEST_DIR)/$(TARGET_DIR)/$(TEST_TARGET)

# 库文件名
STATIC_LIB = $(TARGET_DIR)/lib$(TARGET).a
DYNAMIC_LIB = $(TARGET_DIR)/lib$(TARGET).so

all: release

debug: CFLAGS += -g -fno-omit-frame-pointer -fPIC
debug: CXXFLAGS += -g -fno-omit-frame-pointer -fPIC
debug: $(TARGET_DIR) $(STATIC_LIB) $(DYNAMIC_LIB) Test

release: CFLAGS += -O3 -fno-omit-frame-pointer -fPIC
release: CXXFLAGS += -O3 -fno-omit-frame-pointer -fPIC
release: $(TARGET_DIR) $(STATIC_LIB) $(DYNAMIC_LIB) Test

$(TARGET_DIR):
	mkdir -p $(TARGET_DIR)

# 创建静态库
$(STATIC_LIB): $(OBJECTS)
	ar rcs $@ $^

# 创建动态库
$(DYNAMIC_LIB): $(OBJECTS)
	$(CXX) -shared $(LDFLAGS) -o $@ $^ $(LDLIBS)

# 编译.c文件
build/%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译.cpp文件
build/%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

Test: $(STATIC_LIB) $(DYNAMIC_LIB)
	$(MAKE) -C $(TEST_DIR) LDFLAGS='-L$(abspath $(TARGET_DIR))' LDLIBS='$(LDLIBS) -lframeParse'
# 安装头文件和库

install:
	mkdir -p $(INSTALL_DIR)/include $(INSTALL_DIR)/lib $(INSTALL_DIR)/bin
	cp -p $(STATIC_LIB) $(DYNAMIC_LIB) $(INSTALL_DIR)/lib
	cp -p *.h $(INSTALL_DIR)/include
	cp -p *.hpp $(INSTALL_DIR)/include
	cp -p $(TEST_DIR)/$(TARGET_DIR)/$(TEST_TARGET) $(INSTALL_DIR)/bin

# 清理编译生成的文件
clean:
	rm -rf $(TEST_DIR)/$(TARGET_DIR)
	rm -rf $(TARGET_DIR)
	rm -rf $(INSTALL_DIR)

.PHONY: all clean install
